package jude.java.money.task;

import jude.java.framework.thread.Addressable;
import jude.java.framework.thread.TimeObserver;
import jude.java.util.Utils;

public class TaskDetails implements Addressable, TimeObserver {
	public static final int START = 10000;
	public static final int END = 10001;
	
	private int position;			//位置
	private long lastActiveTime;	//上次活动时间
	private long startTime;			//开始执行时间
	private String threadName;		//线程名
	private String category;		//类别
	private String detail;			//详情
	private String desc;			//描述, 备注, 错误
	
	public TaskDetails() {
		reset();
	}
	
	public void setDetail(String detail) {
		this.detail = detail;
	}

	@Override
	public int getPosition() {
		return position;
	}

	@Override
	public long getLastActiveTime() {
		return lastActiveTime;
	}

	@Override
	public long getStartTime() {
		return startTime;
	}
	
	public void setCategory(String category) {
		this.category = category;
	}
	
	public void setDesc(String desc) {
		this.desc = desc;
	}
	
	public void signal(int position) {
		if (position == 0) {
			this.position = - Math.abs(this.position);
		} else {
			this.position = position;
			this.lastActiveTime = System.currentTimeMillis();
		}
	}

	public void signal(int position, String detail) {
		this.position = position;
		this.lastActiveTime = System.currentTimeMillis();
		this.detail = detail;
	}
	
	public void reset() {
		threadName = Thread.currentThread().getName();
		category = detail = desc = null;
		lastActiveTime = startTime = System.currentTimeMillis();
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(Utils.toTime(System.currentTimeMillis() - lastActiveTime));
		sb.append('\t').append(position);
		if (threadName == null) {
			threadName = Thread.currentThread().getName();
		}
		sb.append("\t'").append(threadName).append('\'');
		if (category != null)
			sb.append('\t').append(category);
		if (detail != null)
			sb.append('\t').append(detail);
		if (desc != null)
			sb.append('\t').append(desc);
		return sb.toString();
	}
}
